// import com.alibaba.druid.sql.ast.*;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.util.ArrayList;
// import java.util.List;

#include "SQLWhileStatement.h"
#include "../../visitor/SQLASTVisitor.h"
#include "../../../utils/listTran.h"

SQLWhileStatement::SQLWhileStatement()
{
  statements = std::make_shared<std::list<SQLStatement_ptr>>();
}

string_ptr SQLWhileStatement::getLabelName()
{
  return labelName;
}

void SQLWhileStatement::setLabelName(string_ptr labelName)
{
  this->labelName = labelName;
}

void SQLWhileStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLWhileStatement)))
  {
    acceptChild(visitor, condition);
    SQLObject_list_ptr tmp1 = ListTran::tran_class<SQLStatement, SQLObject>(statements);
    acceptChild(visitor, tmp1);
  }
  visitor->endVisit(SharedObject(SQLWhileStatement));
}

// @Override
SQLObject_list_ptr SQLWhileStatement::getChildren()
{
  SQLObject_list_ptr children = std::make_shared<std::list<SQLObject_ptr>>();
  children->push_back(condition);
  // children->addAll(this->statements);
  for (auto ele : *this->statements)
  {
    children->push_back(std::dynamic_pointer_cast<SQLObject>(ele));
  }
  return children;
}

SQLStatement_list_ptr SQLWhileStatement::getStatements()
{
  return statements;
}

void SQLWhileStatement::setStatements(SQLStatement_list_ptr statements)
{
  this->statements = statements;
}

SQLExpr_ptr SQLWhileStatement::getCondition()
{
  return condition;
}

void SQLWhileStatement::setCondition(SQLExpr_ptr x)
{
  if (x != nullptr)
  {
    x->setParent(SharedObject(SQLWhileStatement));
  }

  this->condition = x;
}

SQLObject_ptr SQLWhileStatement::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<SQLWhileStatement>(new SQLWhileStatement());
  std::shared_ptr<SQLWhileStatement> x = std::dynamic_pointer_cast<SQLWhileStatement>(clone_tmp);
  // SQLWhileStatement x = new SQLWhileStatement();

  if (condition != nullptr)
  {
    x->setCondition(std::dynamic_pointer_cast<SQLExpr>(condition->clone()));
  }
  for (SQLStatement_ptr stmt : *statements)
  {
    SQLStatement_ptr stmt2 = std::dynamic_pointer_cast<SQLStatement>(stmt->clone());
    stmt2->setParent(x);
    x->statements->push_back(stmt2);
  }
  x->labelName = labelName;
  return x;
}

// @Override
BOOL_ptr SQLWhileStatement::replace(SQLExpr_ptr expr, SQLExpr_ptr target)
{
  if (condition == expr)
  {
    setCondition(target);
    return BOOL::TRUE;
  }

  return BOOL::FALSE;
}
